IF OBJECT_ID('ssf.job_exec_parse_jobstep_command_job_name') IS NOT NULL
	DROP FUNCTION ssf.job_exec_parse_jobstep_command_job_name
GO

CREATE FUNCTION ssf.job_exec_parse_jobstep_command_job_name (
	@jobstep_command NVARCHAR(256)
)
RETURNS NVARCHAR(128)
AS
BEGIN
/*
<object_header>

<object_summary>
	<purpose><![CDATA[
Parse the job_name from a typical jobstep command that uses ssf.job_run_job_as_jobstep to run a job.
	]]></purpose>
</object_summary>

</object_header>
*/

	RETURN (
		CASE WHEN @jobstep_command LIKE N'EXECUTE ssf.job_run_job_as_jobstep%@job_name = ''%'
			THEN SUBSTRING(
				@jobstep_command
				, CHARINDEX(N'@', @jobstep_command)+13
				, CHARINDEX(N'''', @jobstep_command, CHARINDEX(N'@', @jobstep_command)+13) - (CHARINDEX(N'@', @jobstep_command)+13)
			)
			ELSE NULL
			END
	)
END
GO
